package com.xyz.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.xyz.entity.mapper.SystemDictDataMapper;
import com.xyz.entity.mapper.SystemDictTypeMapper;
import com.xyz.entity.mapper.SystemUrlMapper;
import com.xyz.entity.pojo.*;
import com.xyz.service.SystemService;
import com.xyz.util.dto.DataResult;
import com.xyz.util.dto.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional(rollbackFor = Exception.class)
public class SystemServiceImpl implements SystemService {

    @Autowired
    private SystemDictTypeMapper systemDictTypeMapper;

    @Autowired
    private SystemDictDataMapper systemDictDataMapper;

    @Autowired
    private SystemUrlMapper systemUrlMapper;

    @Override
    public DataResult selectDictByDictType(LoginInfo li, String dictTypeCode) throws Exception {
        List<Map<String, Object>> list = new ArrayList<>();
        SystemDictTypeExample sdtExample = new SystemDictTypeExample();
        sdtExample.createCriteria().andDictTypeCodeIn(CollUtil.newArrayList(dictTypeCode.split(",")));
        List<SystemDictType> sdtList = systemDictTypeMapper.selectByExample(sdtExample);

        for (SystemDictType sdt : sdtList) {
            SystemDictDataExample sddExample = new SystemDictDataExample();
            sddExample.createCriteria().andDictTypeIdEqualTo(sdt.getId());
            sddExample.setOrderByClause("dict_sort , dict_type_id");
            List<SystemDictData> sddList = systemDictDataMapper.selectByExample(sddExample);
            for (SystemDictData sdd : sddList) {
                Map<String, Object> map = new HashMap<>();
                map.put("dictValue", sdd.getDictValue());
                map.put("dictName", sdd.getDictName());
                map.put("color", sdd.getColor());
                list.add(map);
            }
        }
        return DataResult.build9100(list);
    }

    @Override
    public DataResult selectBatchDictByDictType(LoginInfo li, String dictTypeCode) throws Exception {
        SystemDictTypeExample sdtExample = new SystemDictTypeExample();
        sdtExample.createCriteria().andDictTypeCodeIn(CollUtil.newArrayList(dictTypeCode.split(",")));
        List<SystemDictType> sdtList = systemDictTypeMapper.selectByExample(sdtExample);
        List<Long> idList = sdtList.stream().map(SystemDictType::getId).collect(Collectors.toList());
        Map<Long, String> dictMap = sdtList.stream().collect(Collectors.toMap(SystemDictType::getId, SystemDictType::getDictTypeCode, (key1, key2) -> key1));
        Map<String, List<SystemDictData>> groupDictMap = new HashMap<>();
        if (CollUtil.isNotEmpty(idList)) {
            SystemDictDataExample sddExample = new SystemDictDataExample();
            sddExample.createCriteria().andDictTypeIdIn(idList);
            sddExample.setOrderByClause("dict_sort , dict_type_id");
            List<SystemDictData> sddList = systemDictDataMapper.selectByExample(sddExample);
            sddList.stream().collect(Collectors.groupingBy(SystemDictData::getDictTypeId))
                    .forEach((k, v) -> {
                        if (dictMap.containsKey(k)) {
                            groupDictMap.put(dictMap.get(k), v);
                        }
                    });
        }
        return DataResult.build9100(groupDictMap);
    }


    @Override
    public DataResult selectSystemLog(LoginInfo li, int pageNum, int pageSize, String username, String beginTime, String endTime) throws Exception {
        Map<String, String> suMap = new HashMap<>();
        SystemUrlExample suExample = new SystemUrlExample();
        suExample.createCriteria().andIsHideEqualTo(0);
        List<SystemUrl> suList = systemUrlMapper.selectByExample(suExample);
        for (SystemUrl su : suList) {
            suMap.put(su.getUrl(), su.getUrlName());
        }


        return null;
    }

}
